<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/6
 * Time: 10:14
 */

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class UserController extends Controller
{
    public function index()
    {
//        $users = DB::select('select * from user where id = ?',[1]);
//        $users = DB::select('select * from user where id = :id',['id' => 1]);
            //获取表中的所有数据
           $users = DB::table('user')->get();
           //分页获取内容
            $users = DB::table('user')->paginate(3);
           //返回某一列的值
//            $users = DB::table('user')->value('name');
            //返回某几列组成的记录，这些记录又组成一个二维数组
//            $users = DB::table('user')->select('name')->get();
//            $users = DB::table('user')->pluck('name');
//            //结果分块，一次处理100条数据
//            DB::table('user')->orderBy('id')->chunk(100, function ($users) {
//                foreach($users as $user)
//                {
//
//                }
//                return false;       //阻止进一步的分块处理，即值返回处理一次
//            });
//
//            //聚合函数的使用
//            $sum = DB::table('user')->count();
//            $price = DB::table('user')->max('orders');
//          dd($users);

        return view('hello',['users' => $users]);
    }

    public function add()
    {
//        $row = DB::insert('insert into user(id, name, password) values(?, ?, ?)',[2,'lisi','123456']);
//        $row = DB::table('user')->insert([
//           'id' => 5,
//            'name' => 'bindi',
//            'password' => '123',
//        ]);
        //插入获取自增id
        $row = DB::table('user')->insertGetId(
            ['id' => 7, 'name' => 'ali', 'password' => '123']
        );
        var_dump($row);
    }

    public function update()
    {
        $row = DB::update('update user set name = ? where id = ?',['zansan',1]);

        //$row = DB::table('user')->where('id',7)->update(['name' => 'jiebing']);
        //自增或者自减
        DB::table('user')->increment('orders',2,['name'=>'ali']);
//        DB::table('user')->decrement('orders');
        var_dump($row);
    }

    public function delete()
    {
        $row = DB::delete('delete from user where id = ?',[1]);
        $row = DB::table('user')->where('id',7)->delete();
        //清空表并重置ID序号
        DB::table('user')->truncate();
        var_dump($row);
    }
    public function hello()
    {
        return 'hello world';
    }

    public function show($age)
    {
        return 'show'.$age;
    }

    public function csrf()
    {
        return view('csrf');
    }

}